package com.example.sport.controller;


import com.example.sport.domain.*;
import com.example.sport.service.*;
import com.example.sport.utils.MD5Utils;
import com.example.sport.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@Tag(name = "裁判相关", description = "这是描述")
@RestController
@RequestMapping("/judge")
@Validated
@Slf4j
//请求值校验
public class JudgeController {


    @Autowired
    private AthleteService athleteService;


    @Operation(summary = "结果查询")
    @GetMapping("/athlete_event_result")
    public Result<List<AthleteEventResult>> getAthleteEvent(
            @RequestParam(required = false, defaultValue = "") String id) {
        //查询用户
        return Result.success(athleteService.getAthleteEventResultsByUserId(id));
    }

    @Autowired
    private JudgeService judgeService;

    @Operation(summary = "学院绩点和排名")
    @GetMapping("/college_rank_points")
    public Result<List<CollegeRankAndPoints>> getCollegeRankAndPoints(
            @RequestParam(required = false, defaultValue = "") String collegeName) {
        //查询用户
        return Result.success(judgeService.getCollegeRankAndPointsByCollegeName(collegeName));
    }

    @Autowired
    private EventService eventService;

    @Operation(summary = "赛事查询")
    @GetMapping("/college_event")
    public Result<List<Event>> getCollegeEvent(
            @RequestParam(required = false, defaultValue = "") String name) {
        //查询用户
        List<Event> list = eventService.getEventByEventName(name);
        return Result.success(list);
    }

    @Operation(summary = "赛事结果查询")
    @GetMapping("/result_by_id")
    public Result<AthleteEventResult> getAthleteEventResultsById(
            @RequestParam(required = false, defaultValue = "") String id) {
        //查询用户
        AthleteEventResult result = judgeService.getAthleteEventResultsById(id);
        return Result.success(result);
    }

    //    @Operation(summary = "学生绩点修改")
//    @PutMapping("/update_athlete_score")
//    public Result updateAthleteScore(
//            @RequestBody EventResult eventResult) {
//        //查询用户
//        judgeService.updateAthleteScore(eventResult);
//        return Result.success();
//    }
    @Operation(summary = "成绩更新/添加")
    @PutMapping("/update_athlete_score")
    public Result updateAthleteScore(@RequestBody EventResult eventResult) {
        if (judgeService.updateResult(eventResult) < 1) {
            judgeService.addResult(eventResult);
            return Result.success("添加成功");
        }
        return Result.success("修改成功");
    }

    @Operation(summary = "运动员查找")
    @GetMapping("/athletes_by_name")
    public Result<List<User>> athleteByName(@RequestParam(required = false, defaultValue = "") String name) {
        return Result.success(athleteService.getUserByRoleAndName(name, "ATHLETE"));
    }
}
